package win.demonlegion.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import win.demonlegion.module.SysUserBean;
import win.demonlegion.service.inte.SysUserService;
import win.demonlegion.util.JwtTokenUtil;
import win.demonlegion.util.ResultUtil;

/**
 * Created by john on 2017/7/18.
 */
@RestController
@RequestMapping("user")
public class UserController {
    private static Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private SysUserService sysUserService;

    @RequestMapping("login")
    public String login(String username, String password) {
        SysUserBean sysUserBean = sysUserService.login(username, password);
        if(sysUserBean != null) {
            return JwtTokenUtil.getUserToken(sysUserBean);
        }
        return null;
    }

    @RequestMapping("auth")
    public String auth(String token) {
        SysUserBean sysUserBean = sysUserService.loadUserByUsername("linfeng");
        if(sysUserBean != null) {
            return ResultUtil.getResult(JwtTokenUtil.validateToken(token, sysUserBean));
        }
        return ResultUtil.getResult(false);
    }
}
